package com.mty.kjtest.Activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;
import com.mty.kjtest.App;
import com.mty.kjtest.MVPView.IUserLoginView;
import com.mty.kjtest.Model.User;
import com.mty.kjtest.Presenter.UserLoginPresenter;
import com.mty.kjtest.R;

public class MainActivity extends AppCompatActivity implements IUserLoginView {


    TextView tv_name, tv_password;
    UserLoginPresenter userLoginPresenter = new UserLoginPresenter(this, this);

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        App.getInstance().onHomeActCreate(this);

        tv_name = (TextView) findViewById(R.id.tv_username);
        tv_password = (TextView) findViewById(R.id.tv_password);
        tv_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                userLoginPresenter.login();
            }
        });


        webView = (WebView) findViewById(R.id.wv_test);

        webView.loadUrl("http://www.baidu.com");

        webView.getSettings().setJavaScriptEnabled(true);//支持JS
        webView.getSettings().setBuiltInZoomControls(true);//缩放
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("weixin://") || url.startsWith("alipays://") ||
                    url.startsWith("mailto://") || url.startsWith("tel://")) {
                    //其他自定义的scheme
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                    return true;
                }
                webView.loadUrl(url);
                return true;
            }

        });



    }


    @Override
    public String getUserName() {
        return tv_name.getText().toString();
    }

    @Override
    public String getPassword() {
        return tv_password.getText().toString();
    }

    @Override
    public void clearUserName() {
        tv_name.setText("");
    }

    @Override
    public void clearPassword() {
        tv_password.setText("");
    }

    @Override
    public void showLoding() {

    }

    @Override
    public void hideLoding() {

    }

    @Override
    public void toMainActivity(User user) {
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedError() {
        Toast.makeText(this, "登录失败", Toast.LENGTH_SHORT).show();
    }

}
